package pe.edu.upc.dew.tddpersistencia.dao;

import pe.edu.upc.dew.tddpersistencia.util.JdbcUtils;
import pe.edu.upc.dew.tddpersistencia.*;
import pe.edu.upc.dew.tddpersistencia.model.Cliente;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Lennon Shimokawa
 */
public class ClienteDaoJdbc implements ClienteDao {

    @Override
    public Cliente getClientePorIdCliente(Integer idCliente) {
        Cliente cliente = null;
        Connection connection = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            connection = JdbcUtils.getConnection();
            st = connection.createStatement();
            final String query = "select * from customer where customer_id=" + idCliente;
            System.out.println("query=" + query);
            rs = st.executeQuery(query);

            if (rs.next()) {
                cliente = new Cliente();
                cliente.setIdCliente(rs.getInt("customer_id"));
                cliente.setNombre(rs.getString("name"));
            }
        } catch (SQLException e) {
            throw new IllegalStateException("Error al obtener el cliente", e);
        } finally {
            JdbcUtils.closeResultSet(rs);
            JdbcUtils.closeStatement(st);
            JdbcUtils.closeConnection(connection);
        }
        return cliente;
    }
}
